<!--
 * @Description: 推荐主页，通过算法整合博文的推荐指数
 * @Descriptiom: 推荐分数 = (100/(博文发布时间和现在的天数差+1))+(评论数 * 5.0) +(是否是好友？是则20.0否则0.0)
 * @Author: 梁晓雨
 * @Date: 2025-03-21 13:46:27
-->
<template>
    <div>
        <el-scrollbar height="98%">
            <BlogItem @goto="toblogMainDetail" v-for="item in BlogItemData" :key="item.blogid" :data="item"></BlogItem>
        </el-scrollbar>
    </div>
</template>
<script lang="ts" setup>
import store from '@/store';
import request from '@/utils/request';
import { ElMessage } from 'element-plus';
import { onMounted, ref } from 'vue';
import BlogItem from "../../../components/BlogItem/BlogItem.vue";
import router from '@/router';
import StringtoStrArray from '@/utils/StringtoStrArray';
import StringtoNumArray from '@/utils/StringtoNumArray';

// 博文数据
interface Blog {
    blogid: number,
    userid: number,
    blogTitle: string,
    aiSummary: string,
    blogText: string,
    blogPhotoSrc: string,
    directoryId: number,
    directoryName: string,
    directorySrc: string,
    blogUpdated: string,
    tagId: Array<number>,
    tagName: Array<string>,
    relayContent: string,
    originalBlogId: number,
    isDeletedBlog: number,
}
// 博客缩略项的数据
const BlogItemData = ref<Blog[]>()

/**
 * @description: 获取推荐博客
 * @description: 推荐分数计算公式：评论数*5+是好友则20否0+时间衰减从100起
 * @Author: 梁晓雨
 * @Date: 2025-03-21 19:40:13
 */
const getBlogRecommend = async () => {
    try {
        const userid = store.getters.getUser.userId;
        await request.get('/blog/queryAllRecommend', {
            params: {
                blog: null,
                userid: userid
            }
        })
            .then(res => {
                res.data.map(item => {
                    item.tagName = StringtoStrArray(item.tagName)
                    item.tagId = StringtoNumArray(item.tagId)
                })
                BlogItemData.value = res.data;

            })
    } catch (error) {
        ElMessage({
            type: 'error',
            message: '获取失败'
        })
    }
}

// 从博客缩略图跳转到博客详情页
interface BlogIdInfo {
    userid: number,
    blogid: number
}
const toblogMainDetail = (blogIdInfo: BlogIdInfo) => {
    router.push({
        name: 'blogMainDetail',
        query: { userid: blogIdInfo.userid, blogid: blogIdInfo.blogid }
    })

}

onMounted(
    async () => {
        await getBlogRecommend();
    }
)
</script>
<style lang="scss" scoped>
@use './BlogRecommedPage.scss';
</style>
